package com.bbs.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bbs.dto.PostAdd;
import com.bbs.dto.PostPreviewDto;
import com.bbs.dto.PostQuery;
import com.bbs.pojo.Post;
import com.bbs.pojo.Result;
import com.bbs.service.IPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/post")
public class PostController {
    @Autowired
    private IPostService iPostService;

    /**
     * 添加帖子
     * @param postAdd
     * @return
     * @throws Exception
     */
    @PostMapping
    @PreAuthorize("isAuthenticated()")
    public String addPost(@RequestBody @Validated PostAdd postAdd) throws Exception {
        System.out.println(iPostService);
       return JSON.toJSONString(iPostService.addPost(postAdd));
    }

    /**
     * 查询帖子
     * @param postQuery
     * @return
     */
    @PostMapping("/query")
    public Result<IPage<PostPreviewDto>> getPosts(@RequestBody PostQuery postQuery){
        return Result.succeed(iPostService.getPosts(postQuery));
    }

    /**
     * 根据id获取帖子
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Post> getPost(@PathVariable("id") String id){
        return Result.succeed(iPostService.getById(id));
    }
}
